<?php
require_once ("BaseDAO.php");

// TableDef Value Object
class TableDefVO extends BaseVO {
    var $field;
    var $type;
    var $isnull;
    var $iskey;
    var $defval;
    var $extra;

    // create a new VO
    function TableDefVO($f = "", $t = "", $n = "", $k = "", $d = "", $x = "") {
        $this->field = $f;
        $this->type = $t;
        $this->isnull = $n;
        $this->iskey = $k;
        $this->defval = $d;
        $this->extra = $x;

    }


    // extra functions here
    function toString() {
        return $this->field . ":" . $this->type . ":" . $this->isnull  . ":" . $this->iskey . ":" . $this->defval . ":" . $this->extra . "\n";
    }

    function isPK() {
        return $this->iskey == "PRI" ? true : false;
    }

    function isNumber() {
        if (substr($this->type, 0 , 7) == "varchar" ||
                substr($this->type, 0 , 4) == "char" ||
                $this->type == "mediumtext" ||
                $this->type == "longtext" ||
                $this->type == "text" ||
                $this->type == "date" ||
                $this->type == "time" ||
                $this->type == "timestamp" ||
                $this->type == "datetime") {
            return false;
        } else {
            return true;
        }
    }

    function isDate() {
        if ($this->type == "timestamp" ||
                $this->type == "datetime") {
            return true;
        } else {
            return false;
        }
    }

    function isPassword() {
    	if ($this->field == "password")
    		return true;
    	return false;
    }
}


// TableDef Data Access Object
class TableDefDAO extends BaseDAO {
    var $SQL_DESCRIBE = "DESCRIBE ";

    // defval constructor
    function TableDefDAO($dbserver="", $dbname="", $dbuser="", $dbpass="") {
        // calls the parent constructor which
        // makes the database connection.
        parent::BaseDAO($dbserver, $dbname, $dbuser, $dbpass);
    }

    // returns an array of TableDefVO objects
    function describe($tablename) {
        $voList = array();
        $this->sql = "DESCRIBE " . $tablename;

        $this->exec($this->sql);
        while($row = $this->getArray()) {
            $vo = new TableDefVO(
                        $row[0],
                        $row[1],
                        $row[2],
                        $row[3],
                        $row[4],
                        $row[5]
                    );
            array_push($voList, $vo);
        }
        return $voList;
    }


}


